package sushil.luc.gui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sushil.luc.item.Item;
import sushil.luc.item.ItemStatus;
import sushil.luc.msc.Customer;
import sushil.luc.msc.DateUtil;
import sushil.luc.msc.MyTicketListAdapter;
import sushil.luc.msc.TruckerCheckManager;
import sushil.luc.ticket.Ticket;
import sushil.luc.ticket.TicketManagerTrucker;
import sushil.luc.truckersmartrfid.R;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;

public class ScanItemFragment  extends Fragment {
	
	private static ListView TicketList;
	private static Context myparent;
	private static TicketManagerTrucker trucker;
	
		 @Override
		    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		        // Assign the layout for this fragment
		    	View view = inflater.inflate(R.layout.activity_main, container, false);
		    	
		    	myparent = container.getContext();
		    	
		    	trucker = new TicketManagerTrucker(myparent);
		    	
		    	// get the Listview with all the tickets and assign on click listener to open details view
		    	TicketList = (ListView) view.findViewById(R.id.ticketlist);
		    	TicketList.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						Intent i = new Intent(myparent,ScanTicketActivity.class);
						// Give the selected Ticket with the intent
						i.putExtra("Position_InList", position);
						startActivity(i);
					}
				});
		    	
		    	return view;
		    }
	
	public void onResume()
	{
		// update the List with the Tickets
		updateList();
		super.onResume();
	}
	/**
	 * Updates the list with all the Tickets
	 */
	private void updateList()
	{
		Log.d("ScanItemFragement", "private updateList");
		TicketList.setAdapter(null);
		
		List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		
		String KEY_LABEL ="Big Text";
		String KEY_HELP ="Help Text";
		
		Map<String, String> group;

		// get all the Tickets which have the status isChecked
		List<Ticket> alltickets = trucker.fetchTickets();
		Log.d("ScanItemFragement", "alltickets"+alltickets.size());
		
		// loop over all the tickets ang get some specific data to be displayed
		for (int i =0; i<alltickets.size();i++)
		{
	    	group = new HashMap<String, String>();
	    	
	    	Ticket tmp = alltickets.get(i);
	    	
	    	Customer c = tmp.getTicketCustomer();

	    	String deliverydate = DateUtil.formatDate(tmp.getDeliveryDate());
	    	// Build the Text to be displayed in the Listview
	    	group.put( KEY_LABEL, "Ticket "+ tmp.getTicketID() );
	    	group.put( KEY_HELP, deliverydate+" | Client "+c.getName() );
	    	
	    	groupData.add(group);
		}
		// create an Adapter which colors the Tickets according to their current status
		MyTicketListAdapter adapter = new MyTicketListAdapter(myparent, groupData, android.R.layout.simple_list_item_2,  new String[] { KEY_LABEL, KEY_HELP },
		                                                   new int[]{ android.R.id.text1, android.R.id.text2 }, alltickets);
		
		TicketList.setAdapter(adapter);
	}
	
	/**
	 * Method to update the Tickets in the Listview, if the request to the database is done concurrently.
	 */
	public static void updateTicketList()
	{
		Log.d("ScanItemFragement", "static updateTicketList");
		TicketList.setAdapter(null);
		
		List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		
		String KEY_LABEL ="Big Text";
		String KEY_HELP ="Help Text";
		
		Map<String, String> group;
		
		// get all the Tickets which have the status isChecked
		List<Ticket> alltickets = trucker.fetchLocalTickets();
		Log.d("ScanItemFragement", "alltickets"+alltickets.size());
		
		// loop over all the tickets ang get some specific data to be displayed
		for (int i =0; i<alltickets.size();i++)
		{
	    	group = new HashMap<String, String>();
	    	
	    	Ticket tmp = alltickets.get(i);
	    	
	    	Customer c = tmp.getTicketCustomer();

	    	String deliverydate = DateUtil.formatDate(tmp.getDeliveryDate());
	    	// Build the Text to be displayed in the Listview
	    	group.put( KEY_LABEL, "Ticket "+ tmp.getTicketID() );
	    	group.put( KEY_HELP, deliverydate+" | Client "+c.getName() );
	
	    	groupData.add(group);
		}
		
		// create an Adapter which colors the Tickets according to their current status
		MyTicketListAdapter adapter = new MyTicketListAdapter(myparent, groupData, android.R.layout.simple_list_item_2,  new String[] { KEY_LABEL, KEY_HELP },
		                                                   new int[]{ android.R.id.text1, android.R.id.text2 }, alltickets);
		
		TicketList.setAdapter(adapter);
	}
}
